package com.sjb.sixteen_journey_backend.controller;


import com.sjb.sixteen_journey_backend.common.R;
import com.sjb.sixteen_journey_backend.entity.*;
import com.sjb.sixteen_journey_backend.entity.vo.MyMapVo;
import com.sjb.sixteen_journey_backend.service.ICommonnessService;
import com.sjb.sixteen_journey_backend.utils.AliOssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 共性表
 *
 * @author jcz
 * @since 2024-07-15
 */
@RestController
@RequestMapping("/commonness")
public class CommonnessController {
    @Autowired
    private ICommonnessService commonnessService;

    /**
     * 上传图片组
     * @param files
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadListImages")
    public R<List<String>> upload(@RequestParam List<MultipartFile> files) throws Exception {
        try {
            List<String> urls = new ArrayList<>();
            for(MultipartFile file : files){
//            把文件的内容存储到本地磁盘上  file.getOriginalFilename文件的原始名字
                String originalFilename = file.getOriginalFilename();
//            保证文件的名字是唯一的,从而防止文件覆盖
                String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

//          存储在本地
//            String realPath = System.getProperty("user.dir") +imagesProperties.getRootPath()+imagesProperties.getAvatar();
//            file.transferTo(new File(realPath + filename));
//            return R.success(imagesProperties.getVisitImagePath()+imagesProperties.getAvatar() + filename, "url访问成功");

//            存储在阿里云云服务器上 oss
                String url = AliOssUtil.uploadFile(filename,file.getInputStream());
                urls.add(url);
            }
            return R.success(urls,"图片上传成功");

        } catch (Exception e) {
            return R.error("图片上传失败！");
        }
    }

    /**
     * 上传一张图片
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    public R<String> upload(@RequestParam MultipartFile file) throws Exception {
        try {
//            把文件的内容存储到本地磁盘上  file.getOriginalFilename文件的原始名字
            String originalFilename = file.getOriginalFilename();
//            保证文件的名字是唯一的,从而防止文件覆盖
            String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

//          存储在本地
//            String realPath = System.getProperty("user.dir") +imagesProperties.getRootPath()+imagesProperties.getAvatar();
//            file.transferTo(new File(realPath + filename));
//            return R.success(imagesProperties.getVisitImagePath()+imagesProperties.getAvatar() + filename, "url访问成功");

//            存储在阿里云云服务器上 oss
            String url = AliOssUtil.uploadFile(filename,file.getInputStream());
            return R.success(url,"图片上传成功");

        } catch (Exception e) {
            return R.error("图片上传失败！");
        }
    }

    /**
     * 收藏或者取消收藏
     *
     * @param status 0此刻，1机票晒单，2酒店晒单，3保险理赔晒单，4其他晒单，5爆料
     * @param id 板块id
     * @return
     */
    @PatchMapping("/addCollection/{status}/{id}")
    public R<?> addCollection(@PathVariable("status") Integer status, @PathVariable("id") Integer id) {
        return commonnessService.addCollection(status, id);
    }

    /**
     * 喜欢或者取消喜欢
     *
     * @param status 0此刻，1机票晒单，2酒店晒单，3保险理赔晒单，4其他晒单，5爆料
     * @param id 板块id
     * @return
     */
    @PatchMapping("/addBeLiked/{status}/{id}")
    public R<?> addBeLiked(@PathVariable("status") Integer status, @PathVariable("id") Integer id) {
        return commonnessService.addBeLiked(status, id);
    }

    /**
     * 用户对某篇文的收藏与喜欢情况(好像没有用到)
     *
     * @return
     */
    @GetMapping("/getCollectionAndLikedByModuleId/{idStatus}/{moduleId}")
    public R getCollectionAndLikedByModuleId(@PathVariable("idStatus") Integer idStatus, @PathVariable("moduleId") Integer moduleId) {
        return commonnessService.getCollectionAndLikedByModuleId(idStatus, moduleId);
    }

    /**
     * 获取用户收藏地图
     * @return
     */
    @GetMapping("/showCollectionMap")
    public R<List<MyMapVo>> showCollectionMap() {
        return commonnessService.showCollectionMap();
    }

    /**
     * 获取用户喜欢地图
     * @return
     */
    @GetMapping("/showBeLikedMap")
    public R<List<MyMapVo>> showBeLikedMap() {
        return commonnessService.showBeLikedMap();
    }

    /**
     * 获取用户收藏此刻
     * @return
     */
    @GetMapping("/showCollectionMyTime")
    public R<List<MyTime>> showCollectionMyTime() {
        return commonnessService.showCollectionMyTime();
    }

    /**
     * 获取用户喜欢此刻
     * @return
     */
    @GetMapping("/showBeLikedMyTime")
    public R<List<MyTime>> showBeLikedMyTime() {
        return commonnessService.showBeLikedMyTime();
    }

    /**
     * 获取用户收藏机票晒单
     * @return
     */
    @GetMapping("/showCollectionAirTicker")
    public R<List<AirTicket>> showCollectionAirTicker() {
        return commonnessService.showCollectionAirTicker();
    }

    /**
     * 获取用户喜欢机票晒单
     * @return
     */
    @GetMapping("/showBeLikedAirTicker")
    public R<List<AirTicket>> showBeLikedTicker() {
        return commonnessService.showBeLikedAirTicker();
    }

    /**
     * 获取用户收藏酒店晒单
     * @return
     */
    @GetMapping("/showCollectionHotelTicker")
    public R<List<HotelTicket>> showCollectionHotelTicker() {
        return commonnessService.showCollectionHotelTicker();
    }

    /**
     * 获取用户喜欢酒店晒单
     * @return
     */
    @GetMapping("/showBeLikedHotelTicker")
    public R<List<HotelTicket>> showBeLikedHotelTicker() {
        return commonnessService.showBeLikedHotelTicker();
    }

    /**
     * 获取用户收藏保险理赔晒单
     * @return
     */
    @GetMapping("/showCollectionInsuranceTicker")
    public R<List<InsuranceTicket>> showCollectionInsuranceTicker() {
        return commonnessService.showCollectionInsuranceTicker();
    }

    /**
     * 获取用户喜欢保险理赔晒单
     * @return
     */
    @GetMapping("/showBeLikedInsuranceTicker")
    public R<List<InsuranceTicket>> showBeLikedInsuranceTicker() {
        return commonnessService.showBeLikedInsuranceTicker();
    }

    /**
     * 获取用户收藏其他晒单
     * @return
     */
    @GetMapping("/showCollectionOthersTicker")
    public R<List<OthersTicket>> showCollectionOthersTicker() {
        return commonnessService.showCollectionOthersTicker();
    }

    /**
     * 获取用户喜欢其他晒单
     * @return
     */
    @GetMapping("/showBeLikedOthersTicker")
    public R<List<OthersTicket>> showBeLikedOthersTicker() {
        return commonnessService.showBeLikedOthersTicker();
    }

    /**
     * 获取用户收藏爆料
     * @return
     */
    @GetMapping("/showCollectionShareThings")
    public R<List<ShareThing>> showCollectionShareThings() {
        return commonnessService.showCollectionShareThings();
    }

    /**
     * 获取用户喜欢爆料
     * @return
     */
    @GetMapping("/showBeLikedShareThings")
    public R<List<ShareThing>> showBeLikedShareThings() {
        return commonnessService.showBeLikedShareThings();
    }


    /**
     * 获取用户收藏的约伴
     * @return
     */
    @GetMapping("/showCollectionComeFriend")
    public R<List<ComeFriend>> showCollectionComeFriend() {
        return commonnessService.showCollectionComeFriend();
    }


    /**
     * 根据关键字搜索文章
     * @param keywords
     * @return
     * @param <T>
     */
    @GetMapping("/showByKeywords")
    public <T> R<List<List<T>>> showByKeywords(@RequestParam("keywords") String keywords) {
        return commonnessService.showByKeywords(keywords);
    }

    /**
     * 根据城市名搜索我的此刻和地图
     * @param keywords
     * @return
     * @param <T>
     */
    @GetMapping("/showByCityName")
    public <T> R<List<List<T>>> showByCityName(@RequestParam("keywords") String keywords) {
        return commonnessService.showByCityName(keywords);
    }

//    /**
//     * 删除用户的某篇文
//     *
//     * @param status
//     * @param id
//     * @return
//     */
//    @DeleteMapping("/deleteArticle/{status}/{id}")
//    public R deleteMyTimeById(@PathVariable("status") Integer status,
//                              @PathVariable("id") Integer id) {
//        return commonnessService.deleteMyTimeById(status, id);
//    }



}
